package com.astronomy.spit.controller;

import com.astronomy.entity.PageResult;
import com.astronomy.entity.Result;
import com.astronomy.entity.StatusCode;
import com.astronomy.spit.pojo.Spit;
import com.astronomy.spit.service.SpitService;
import com.astronomy.states.Status;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * Copyright(C),2019-2020,CarryWLTao互联网工作室
 * FileName:SpitController
 * Author:  Administrator
 * Date:    2020-06-19 14:08
 * Description:
 * History:
 * <author>     <time>      <version>       <desc>
 * 作者姓名     修改时间       版本号          描述
 */
@RestController
@CrossOrigin
@RequestMapping("/spit")
public class SpitController {
    public static final String THUMBUP_KEY="thumbup";
    @Autowired
    private SpitService spitService;
    @Autowired
    private RedisTemplate redisTemplate;
    /***
     * 查询全部数据
     *  @return
     *  */
    @GetMapping
    public Result findAll() {
        return new Result(true, StatusCode.OK, Status.SELECT_SUCCESS, spitService.findAll());
    }

    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable String id) {
        return new Result(true, StatusCode.OK, Status.SELECT_SUCCESS, spitService.findById(id));
    }

    @PostMapping
    public Result add(@RequestBody Spit spit) {
        spitService.save(spit);
        return new Result(true, StatusCode.OK, Status.ADD_SUCCESS);
    }

    @PutMapping("/{id}")
    public Result update(@RequestBody Spit spit, @PathVariable String id) {
        spit.set_id(id);
        spitService.update(spit);
        return new Result(true, StatusCode.OK, Status.UPDATE_SUCCESS);
    }
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable String id) {
        spitService.deleteById(id);
        return new Result(true, StatusCode.OK, Status.DELETE_SUCCESS);
    }
    @GetMapping("/comment/{parentid}/{page}/{size}")
    public Result findByParentid(@PathVariable String parentid,@PathVariable int page,@PathVariable int size){
        Page<Spit> spitPage = spitService.findByParentid(parentid, page, size);
        return new Result(true,StatusCode.OK,Status.SELECT_SUCCESS,new PageResult<Spit>(spitPage.getTotalElements(),spitPage.getContent()));
    }

    /**
     * 点赞
     * @param spitId
     * @return
     */
    @PutMapping("/thumbup/{spitId}")
    public Result thumbup(@PathVariable String spitId){
        //判断当前用户是否已经点赞
        String userid="111";
        //如果已经点赞直接返回不允许重复点赞否则点赞写回redis
       if(redisTemplate.opsForValue().get(THUMBUP_KEY+userid)!=null){
           return new Result(false,StatusCode.REPERROR,Status.NOT_REPEAT);
       }
        spitService.thumbup(spitId);
       redisTemplate.opsForValue().set(THUMBUP_KEY+userid,1);
        return new Result(true,StatusCode.OK,Status.THUMBUO_SUCCESS);
    }

}
